package com.zxbit.flash.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zxbit.flash.R;
import com.zxbit.flash.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class LocationDialog extends Dialog {

    private View rootView;
    private TextView tv_title;
    private RecyclerView recycler_view;
    private MenuListAdapter listAdapter;
    private TextView mTempeditText;
    private TextView mTempeditText2;
    private int viewType;

    public LocationDialog(@NonNull Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.layout_of_menu_dialog, null);
        tv_title = rootView.findViewById(R.id.tv_title);
        recycler_view = rootView.findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler_view.setLayoutManager(linearLayoutManager);
        setContentView(rootView);

        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.select_dialog_animation);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, 906);//设置横向全屏

    }

    public LocationDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    protected LocationDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initView(context);
    }

    public void showDialog(TextView editText,ArrayList<String> itemList){
        mTempeditText = editText;
        if (itemList == null || itemList.size() == 0) return;
        if (recycler_view != null) {
            if (listAdapter == null) {
                listAdapter = new MenuListAdapter(itemList, getContext());
                recycler_view.setAdapter(listAdapter);

            } else {
                listAdapter.setData(itemList);
                listAdapter.notifyDataSetChanged();
            }
        }

        show();
    }


    public void showDialog(String titleStr, ArrayList<String> itemList, TextView editText, TextView editText2) {
        mTempeditText = editText;
        mTempeditText2 = editText2;
        if (itemList == null || itemList.size() == 0) return;
        if (tv_title != null) {
            tv_title.setText(titleStr);
        }
        if (recycler_view != null) {
            if (listAdapter == null) {
                listAdapter = new MenuListAdapter(itemList, getContext());
                recycler_view.setAdapter(listAdapter);

            } else {
                listAdapter.setData(itemList);
                listAdapter.notifyDataSetChanged();
            }
        }

        show();
    }

    public class MenuListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final Context mContext;
        private List<String> mItemList;

        public MenuListAdapter(ArrayList<String> itemList, Context context) {
            this.mItemList = itemList;
            this.mContext = context;
        }




        public void setData(ArrayList<String> itemList) {
            this.mItemList = itemList;

        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            if (mContext == null) return null;
            TypedValue typedValue = new TypedValue();
            getContext().getTheme().resolveAttribute(R.attr.menu_dialog_selectableItemBackground, typedValue, true);
            View itemView;
            if (viewType == 0) {
                itemView = LayoutInflater.from(mContext).inflate(R.layout.layout_of_menu_item, parent, false);
                itemView.setBackgroundResource(typedValue.resourceId);
                return new MenuListViewHolder(itemView);
            } else {
                itemView = LayoutInflater.from(mContext).inflate(R.layout.layout_of_menu_contacts_item, parent, false);
                itemView.setBackgroundResource(typedValue.resourceId);
                return new MenuListContactsViewHolder(itemView);
            }
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
            if (!StringUtils.isEmpty(mItemList.get(position))) {
                if (holder instanceof  MenuListViewHolder) {
                    MenuListViewHolder viewHolder = (MenuListViewHolder) holder;
                    viewHolder.tv_item_text.setText(mItemList.get(position));
                    viewHolder.layout_container.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mTempeditText != null) {
                                mTempeditText.setText(mItemList.get(position));
                                if (!StringUtils.isEmpty(mItemList.get(position))) {
                                    mTempeditText.setTag(mItemList.get(position));
                                }
                                if (mTempeditText2 != null) {
                                    mTempeditText2.setText(mItemList.get(position));
                                }
                                LocationDialog.this.dismissDialog();
                            }
                        }
                    });
                } else if (holder instanceof  MenuListContactsViewHolder) {
                    MenuListContactsViewHolder viewHolder = (MenuListContactsViewHolder) holder;
                    viewHolder.tv_item_text_name.setText(mItemList.get(position));
                    viewHolder.tv_item_text_number.setText(mItemList.get(position));
                    viewHolder.layout_container.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mTempeditText != null) {
                                if (!StringUtils.isEmpty(mItemList.get(position))) {
                                    mTempeditText.setTag(mItemList.get(position));
                                }
                                mTempeditText.setText(mItemList.get(position));
                                if (mTempeditText2 != null) {
                                    mTempeditText2.setText(mItemList.get(position));
                                }
                                LocationDialog.this.dismissDialog();
                            }
                        }
                    });
                }
            }
        }

        @Override
        public int getItemCount() {
            if (mItemList == null) return 0;
            return mItemList.size();
        }

        @Override
        public int getItemViewType(int position) {
            return viewType;
        }
    }

    public void dismissDialog() {
        if (isShowing()) dismiss();
    }

    public class MenuListViewHolder extends RecyclerView.ViewHolder {

        LinearLayout layout_container;
        TextView tv_item_text;

        public MenuListViewHolder(View itemView) {
            super(itemView);
            layout_container = itemView.findViewById(R.id.layout_container);
            tv_item_text = itemView.findViewById(R.id.tv_item_text);
        }

    }

    public class MenuListContactsViewHolder extends RecyclerView.ViewHolder {

        LinearLayout layout_container;
        TextView tv_item_text_name;
        TextView tv_item_text_number;

        public MenuListContactsViewHolder(View itemView) {
            super(itemView);
            layout_container = itemView.findViewById(R.id.layout_container);
            tv_item_text_name = itemView.findViewById(R.id.tv_item_text_name);
            tv_item_text_number = itemView.findViewById(R.id.tv_item_text_number);
        }

    }
}
